package com.free.semantic.infrastruct.repository.impl;

import com.free.semantic.infrastruct.common.RelationImplDO;
import com.free.semantic.infrastruct.mapper.RelationImplMapper;
import com.free.semantic.infrastruct.repository.RelationImplRepository;
import com.free.semantic.model.conceptmodel.RelationImplEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
public class RelationImplRepositoryImpl implements RelationImplRepository {

    @Autowired
    private RelationImplMapper relationImplMapper;

    @Override
    public RelationImplEntity findById(Long id) {
        RelationImplDO implDO = relationImplMapper.findById(id);
        return convertToModel(implDO);
    }

    @Override
    public List<RelationImplEntity> findBySemanticNetworkId(Long semanticNetworkId) {
        List<RelationImplDO> implDOs = relationImplMapper.findBySemanticNetworkId(semanticNetworkId);
        return implDOs.stream()
                .map(this::convertToModel)
                .collect(Collectors.toList());
    }

    @Override
    public List<RelationImplEntity> findByRelationItemId(Long relationItemId) {
        List<RelationImplDO> implDOs = relationImplMapper.findByRelationItemId(relationItemId);
        return implDOs.stream()
                .map(this::convertToModel)
                .collect(Collectors.toList());
    }

    @Override
    public Long save(RelationImplEntity relationImplEntity) {
        RelationImplDO implDO = convertToDO(relationImplEntity);
        relationImplMapper.insert(implDO);
        return implDO.getId();
    }

    @Override
    public void update(RelationImplEntity relationImplEntity) {
        RelationImplDO implDO = convertToDO(relationImplEntity);
        relationImplMapper.update(implDO);
    }

    @Override
    public void deleteById(Long id) {
        relationImplMapper.deleteById(id);
    }

    @Override
    public void deleteBySemanticNetworkId(Long semanticNetworkId) {
        relationImplMapper.deleteBySemanticNetworkId(semanticNetworkId);
    }

    @Override
    public void deleteByRelationItemId(Long relationItemId) {
        relationImplMapper.deleteByRelationItemId(relationItemId);
    }

    private RelationImplEntity convertToModel(RelationImplDO implDO) {
        if (implDO == null) {
            return null;
        }
        RelationImplEntity impl = new RelationImplEntity();
        impl.setId(implDO.getId());
        impl.setSemanticNetworkId(implDO.getSemanticNetworkId());
        impl.setRelationItemId(implDO.getRelationItemId());
        impl.setImplStr(implDO.getImplStr());
        return impl;
    }

    private RelationImplDO convertToDO(RelationImplEntity impl) {
        if (impl == null) {
            return null;
        }
        RelationImplDO implDO = new RelationImplDO();
        implDO.setId(impl.getId());
        implDO.setSemanticNetworkId(impl.getSemanticNetworkId());
        implDO.setRelationItemId(impl.getRelationItemId());
        implDO.setImplStr(impl.getImplStr());
        return implDO;
    }
} 